---@diagnostic disable: undefined-global
---
--- Created by PengZhang.
--- DateTime: 2025/9/23 20:29
---
--优惠卷ID
local voucherId = ARGV[1]
--用户id
local userId = ARGV[2]

--秒杀库存
local stockKey = 'seckill:stock' .. voucherId
--订单key
local orderKey = 'seckill:order' .. voucherId

--判断库存是否充足
if (tonumber(redis.call("get", stockKey)) <= 0) then
    --库存不足返回1
    return 1
end
--判断用户是否已经下单
if (redis.call("sismember", orderKey, userId) == 1) then
    --已经下单返回2
    return 2
end
--库存扣减
redis.call("incrby", stockKey,-1)
--记录下单
redis.call("sadd", orderKey, userId)
--下单成功返回0
return 0


